package cc.rengu.igas.bsps.biz.service.impl;

import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.facade.MchntOrderService;
import cc.rengu.igas.bsps.facade.request.CustomerStatisticRequest;
import cc.rengu.igas.bsps.facade.request.MchntOrderInfoRequest;
import cc.rengu.igas.bsps.facade.response.CustomerStatisticResponse;
import cc.rengu.igas.bsps.facade.response.MchntOrderInfoResponse;
import cc.rengu.igas.bsps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * @author wpf
 * @version 1.0
 * @description
 * @Date 2023/12/20 13:26
 */
public class MchntOrderServiceImpl implements MchntOrderService {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<CustomerStatisticResponse> customerStatistic(CustomerStatisticRequest customerStatisticRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CustomerStatisticResponse> result = new Result<>();
        CustomerStatisticResponse customerStatisticResponse = new CustomerStatisticResponse();
        customerStatisticResponse.setHeader(customerStatisticRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(customerStatisticRequest, CustomerStatisticResponse.class);
            customerStatisticResponse = (CustomerStatisticResponse) dubboResult.getResult();
            customerStatisticResponse.setRespCode(dubboResult.getRespCode());
            customerStatisticResponse.setRespDesc(dubboResult.getRespDesc());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("顾客统计表操作失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException){
                customerStatisticResponse.setRespCode(((BizException) e).getCode());
                customerStatisticResponse.setRespDesc(e.getMessage());
            }else {
                customerStatisticResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                customerStatisticResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        result.setResult(customerStatisticResponse);
        return result;
    }

    @Override
    public Result<MchntOrderInfoResponse> getMchntOrderList(MchntOrderInfoRequest mchntOrderInfoRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntOrderInfoResponse> result = new Result<>();
        MchntOrderInfoResponse mchntOrderInfoResponse = new MchntOrderInfoResponse();
        mchntOrderInfoResponse.setHeader(mchntOrderInfoRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntOrderInfoRequest, MchntOrderInfoResponse.class);
            mchntOrderInfoResponse = (MchntOrderInfoResponse) dubboResult.getResult();
            mchntOrderInfoResponse.setRespCode(dubboResult.getRespCode());
            mchntOrderInfoResponse.setRespDesc(dubboResult.getRespDesc());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户订单表查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException){
                mchntOrderInfoResponse.setRespCode(((BizException) e).getCode());
                mchntOrderInfoResponse.setRespDesc(e.getMessage());
            }else {
                mchntOrderInfoResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntOrderInfoResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        result.setResult(mchntOrderInfoResponse);
        return result;
    }
}
